package features.advance.leetcode.dynamicProgramming.easy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LIN
 * @date 2022-04-29 21:29
 *  119. 杨辉三角 II
 * 给定一个非负索引 rowIndex，返回「杨辉三角」的第 rowIndex 行。
 *
 * 在「杨辉三角」中，每个数是它左上方和右上方的数的和。
 *
 *
 *
 *
 *
 * 示例 1:
 *
 * 输入: rowIndex = 3
 * 输出: [1,3,3,1]
 * 示例 2:
 *
 * 输入: rowIndex = 0
 * 输出: [1]
 * 示例 3:
 *
 * 输入: rowIndex = 1
 * 输出: [1,1]
 *
 *
 * 提示:
 *
 * 0 <= rowIndex <= 33
 */
public class Solution119 {

    public static void main(String[] args) {
        Solution solution = new Solution() {
        };
        int[] rowIndexs = {0,1,3};
        for (int rowIndex : rowIndexs) {
            /*
                滚动数组 O(rowIndex*rowIndex)
             */
            solution.run(r -> {
                List<Integer> preLine = new ArrayList<>();
                for (int i = 0; i <= r; i++) {
                    List<Integer> curLine = new ArrayList<>();
                    for (int j = 0; j <= i; j++) {
                        if(j==0 || j==i){
                            curLine.add(1);
                        }else{
                            curLine.add(preLine.get(j-1)+preLine.get(j));
                        }
                    }
                    preLine = curLine;
                }
                return preLine;
            },rowIndex, a->{
                System.out.println(a);
            });
        }
        /*
           线性递推，组合数C(m,n)=n!/((n-m)!*m!)   O(rowIndex)
         */
        solution.run(rowIndex -> {
            List<Integer> row = new ArrayList<>();
            row.add(1);

            for (int i = 1; i <= rowIndex; i++) {
                row.add((int) ((long) row.get(i - 1) * (rowIndex - i + 1) / i));
            }
            return row;
        },3,a->{
            System.out.println(a);
        });




    }

    static class Solution {
        public void run(Sol solution, int rowIndex, Answer answer){
            answer.answer(solution.getRow(rowIndex));
        }
    }

    @FunctionalInterface
    interface Sol{
        List<Integer> getRow(int rowIndex);
    }

    @FunctionalInterface
    interface Answer{
        void answer(List<Integer> a);
    }
}
